//package ru.tvsamara.staff.Configuration;
//
//import com.nimbusds.jose.crypto.DirectDecrypter;
//import com.nimbusds.jose.crypto.DirectEncrypter;
//import com.nimbusds.jose.jwk.OctetSequenceKey;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.http.HttpMethod;
//import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.security.config.Customizer;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
//import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
//import org.springframework.security.config.http.SessionCreationPolicy;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.security.web.SecurityFilterChain;
//import org.springframework.security.web.access.ExceptionTranslationFilter;
//import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
//import org.springframework.security.web.csrf.CsrfTokenRequestAttributeHandler;
//import ru.tvsamara.staff.Security.Token.GetCsrfTokenFilter;
//import ru.tvsamara.staff.Security.Token.TokenCookieAuthenticationConfigurer;
//import ru.tvsamara.staff.Security.Token.TokenCookieJweStringDeserializer;
//import ru.tvsamara.staff.Security.Token.TokenCookieJweStringSerializer;
//import ru.tvsamara.staff.Security.Token.TokenCookieSessionAuthenticationStrategy;
//import ru.tvsamara.staff.repository.auth.UserRepository;
//import ru.tvsamara.staff.service.auth.impl.UserDetailsServiceImpl;
//
//
///**
// *
// * @author venia
// */
//@Configuration
//@EnableWebSecurity
//public class ConfigsBeans {
//        @Bean
//    public TokenCookieJweStringSerializer tokenCookieJweStringSerializer(
//            @Value("${jwt.cookie-token-key}") String cookieTokenKey
//    ) throws Exception {
//        return new TokenCookieJweStringSerializer(new DirectEncrypter(
//                OctetSequenceKey.parse(cookieTokenKey)
//        ));
//    }
//    
//        @Bean
//    public TokenCookieAuthenticationConfigurer tokenCookieAuthenticationConfigurer(
//            @Value("${jwt.cookie-token-key}") String cookieTokenKey,
//            JdbcTemplate jdbcTemplate
//    ) throws Exception {
//        return new TokenCookieAuthenticationConfigurer()
//                .tokenCookieStringDeserializer(new TokenCookieJweStringDeserializer(
//                        new DirectDecrypter(
//                                OctetSequenceKey.parse(cookieTokenKey)
//                        )
//                ))
//                .jdbcTemplate(jdbcTemplate);
//    }
//       @Bean
//    public SecurityFilterChain securityFilterChain(
//            HttpSecurity http,
//            TokenCookieAuthenticationConfigurer tokenCookieAuthenticationConfigurer,
//            TokenCookieJweStringSerializer tokenCookieJweStringSerializer) throws Exception {
//        var tokenCookieSessionAuthenticationStrategy = new TokenCookieSessionAuthenticationStrategy();
//        tokenCookieSessionAuthenticationStrategy.setTokenStringSerializer(tokenCookieJweStringSerializer);
//
//        http.httpBasic(Customizer.withDefaults())
//                .formLogin(Customizer.withDefaults())
//                .addFilterAfter(new GetCsrfTokenFilter(), ExceptionTranslationFilter.class)
//                .authorizeHttpRequests(authorizeHttpRequests ->
//                        authorizeHttpRequests
//                                .requestMatchers("/index.html", "/index").hasRole("USER")
//                                .requestMatchers( "/upload/uploadempl").hasRole("ANONYMOUS").anyRequest().permitAll()
//                                //.anyRequest().authenticated()
//                ).csrf(AbstractHttpConfigurer::disable)
//                .sessionManagement(sessionManagement -> sessionManagement
//                        .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
//                        .sessionAuthenticationStrategy(tokenCookieSessionAuthenticationStrategy))
//                .csrf(csrf -> csrf.csrfTokenRepository(new CookieCsrfTokenRepository())
//                        .csrfTokenRequestHandler(new CsrfTokenRequestAttributeHandler())
//                        .sessionAuthenticationStrategy((authentication, request, response) -> {}));
//
//        http.apply(tokenCookieAuthenticationConfigurer);
//
//        return http.build();
//    }
//    
//        @Bean
//    public UserDetailsService userDetailsService(UserRepository userRepo) {
//        return new UserDetailsServiceImpl();
//    }
//       @Bean
//    public PasswordEncoder passwordEncoder(){
//        return new BCryptPasswordEncoder();
//    }
//
//}
